home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2000 #5 / Amiga Plus CD - 2000 - No. 5.iso / Tools / Dev / GameboyDev / GBDK / lib / sqrt.ms < prev    next >
Encoding:
Text File  |  1999-03-29  |  902 b   |  77 lines

  1.     INCLUDE "macros.ms"
  2.     .include "math.s"
  3.  
  4.     .area    _BSS
  5. tmp:
  6. x:
  7.     .ds    4
  8. og:
  9.     .ds    4
  10. niter:
  11.     .ds    1
  12.  
  13.     .area    _CODE
  14. .sqrt::
  15. ;    og = x;
  16.     storef    x
  17.     storef    og
  18. ;    if(og < 1.0)
  19. ;        og = 1.0/og;
  20.  
  21. ;    og = frexp(og, &exp);
  22. ;    og = ldexp(og, exp/2);    - basically half the exponent
  23.  
  24.     ld    a,h
  25.     sub    #64
  26.     rr    a
  27.     add    #64
  28.     ld    h,a
  29.     
  30. ;    if(x < 1.0)
  31. ;        og = 1.0/og;
  32.     
  33. ;    for(niter = 0 ; niter < 20 ; niter++) {
  34.     ld    a,#5
  35.     ld    (niter),a
  36. start_for:
  37. ;        ng = (x/og + og)/2.0;
  38.     pushf    og
  39.     fetchf    x
  40.     call    .fdiv32
  41.     pushf    og
  42.     call    .fadd32
  43.  
  44.     ; Divide by 2 - decrease mantissa by 1
  45.     dec    h
  46.  
  47. ;        if(ng == og)
  48. ;            break;
  49. ;        og = ng;
  50.     ; See if the result has converged
  51. ;    ld    a,(og)        ; Guess that e has the highest entropy
  52. ;    cp    e
  53. ;    jr    nz,exit_for
  54.     ld    a,(og+1)
  55.     cp    d
  56.     jr    nz,exit_for
  57.     ld    a,(og+2)
  58.     cp    l
  59.     jr    nz,exit_for
  60.     ld    a,(og+3)
  61.     cp    h
  62.     jr    nz,exit_for
  63.     ret
  64.     
  65. exit_for:
  66.     storef    og
  67.  
  68.     ld    a,(niter)
  69.     dec    a
  70.     ld    (niter),a
  71.     jr    nz,start_for
  72. ;    }
  73. ;    return og;
  74. ;}
  75.     ret
  76.  
  77.